package com.copa.l1658;

import java.util.Arrays;

/**
 * @author copa
 * @createDate 2023-01-07 15:03
 * @function 双指针法
 */
public class Test1 {

    public int minOperations(int[] nums, int x) {
        int sum = Arrays.stream(nums).sum() - x;
        if (sum < 0) {
            return -1;
        }
        int left = 0;
        int tempSum = 0;
        int other = -1;
        int n = nums.length;
        for (int right = 0; right < n; right++) {
            tempSum += nums[right];
            while (tempSum > sum) {
                tempSum -= nums[left++];
            }
            if (tempSum == sum) {
                other = Math.max(other, right - left + 1);
            }
        }
        return other < 0 ? -1 : n - other;
    }

    public static void main(String[] args) {
        int[] nums = {1,1,4,2,3};
        int x = 5;
        System.out.println(new Test1().minOperations(nums, x));
    }
}
